﻿using System;
using System.Collections.Generic;
using System.Text;
using System.IO;
using OracleDataToRedis.Utils;
using OracleDataToRedis.Domain;

namespace OracleDataToRedis.Coders.Persistence
{
    public class EntityDaoTransationCoder
    {
        public static void Write()
        {
            string path = BaseParams.PersistencePath;
            if (Directory.Exists(path) == false)
            {
                Directory.CreateDirectory(path);
            }
            path = Path.Combine(path, "EntityDao.Transation.cs");

            #region 创建cs文件
            FileStream file = new FileStream(path, FileMode.Create);
            StreamWriter sw = new StreamWriter(file, Encoding.UTF8);
            CommentsCoder.CreateCsComments("NH持久层事物操作类", sw);

            sw.WriteLine("using System;");
            sw.WriteLine("using System.Data;");
            sw.WriteLine("using System.Collections.Generic;");
            sw.WriteLine("using System.Collections;");
            sw.WriteLine("");
            sw.WriteLine("using NHibernate;");
            sw.WriteLine("using " + BaseParams.DomainNameSpace + ";");
            sw.WriteLine("");
            sw.WriteLine("namespace " + BaseParams.PersistenceNameSpace + "");
            sw.WriteLine("{");
            sw.WriteLine("    public partial class EntityDao<T> where T : BaseEntity");
            sw.WriteLine("    {");
            sw.WriteLine("        /// <summary>");
            sw.WriteLine("        /// 保存对象或更新对象");
            sw.WriteLine("        /// </summary>");
            sw.WriteLine("        /// <param name=\"entity\">需要保存的对象</param>");
            sw.WriteLine("        /// <param name=\"session\">NHibernate会话对象</param>");
            sw.WriteLine("        public void SaveOrUpdate(T entity, ISession session)");
            sw.WriteLine("        {");
            sw.WriteLine("            if (entity.DataCreateTime == null)");
            sw.WriteLine("                entity.DataCreateTime = System.DateTime.Now;");
            sw.WriteLine("            entity.DataUpdateTime = System.DateTime.Now;");
            sw.WriteLine("");
            sw.WriteLine("            session.SaveOrUpdate(entity);");
            sw.WriteLine("        }");
            sw.WriteLine("");
            sw.WriteLine("        /// <summary>");
            sw.WriteLine("        /// 保存对象对象");
            sw.WriteLine("        /// </summary>");
            sw.WriteLine("        /// <param name=\"entity\">需要保存的对象</param>");
            sw.WriteLine("        public object Save(T entity, ISession session)");
            sw.WriteLine("        {");
            sw.WriteLine("            if (entity.DataCreateTime == null)");
            sw.WriteLine("                entity.DataCreateTime = System.DateTime.Now;");
            sw.WriteLine("            entity.DataUpdateTime = System.DateTime.Now;");
            sw.WriteLine("");
            sw.WriteLine("            return session.Save(entity);");
            sw.WriteLine("        }");
            sw.WriteLine("");
            sw.WriteLine("        /// <summary>");
            sw.WriteLine("        /// 保存对象或更新对象集合");
            sw.WriteLine("        /// </summary>");
            sw.WriteLine("        /// <param name=\"entitys\">需要保存的对象集合</param>");
            sw.WriteLine("        /// <param name=\"session\">NHibernate会话对象</param>");
            sw.WriteLine("        public void SaveOrUpdate(IList<T> entitys, ISession session)");
            sw.WriteLine("        {");
            sw.WriteLine("            DateTime dtNow = DateTime.Now;");
            sw.WriteLine("            foreach (T entity in entitys)");
            sw.WriteLine("            {");
            sw.WriteLine("                if (entity.DataCreateTime == null)");
            sw.WriteLine("                    entity.DataCreateTime = dtNow;");
            sw.WriteLine("                entity.DataUpdateTime = dtNow;");
            sw.WriteLine("");
            sw.WriteLine("                session.SaveOrUpdate(entity);");
            sw.WriteLine("            }");
            sw.WriteLine("        }");
            sw.WriteLine("");
            sw.WriteLine("        /// <summary>");
            sw.WriteLine("        /// 更新对象");
            sw.WriteLine("        /// </summary>");
            sw.WriteLine("        /// <param name=\"entity\">需要更新的对象</param>");
            sw.WriteLine("        /// <param name=\"key\">更新对象的原始主键</param>");
            sw.WriteLine("        /// <param name=\"session\">NHibernate会话对象</param>");
            sw.WriteLine("        public void Update(T entity, object key, ISession session)");
            sw.WriteLine("        {");
            sw.WriteLine("            if (entity.DataCreateTime == null)");
            sw.WriteLine("                entity.DataCreateTime = System.DateTime.Now;");
            sw.WriteLine("            entity.DataUpdateTime = System.DateTime.Now;");
            sw.WriteLine("");
            sw.WriteLine("            if (!entity.EntityID.Equals(key))");
            sw.WriteLine("            {");
            sw.WriteLine("                session.Delete(session.Get(typeof(T), key));");
            sw.WriteLine("                session.Save(entity);");
            sw.WriteLine("            }");
            sw.WriteLine("            else");
            sw.WriteLine("            {");
            sw.WriteLine("                session.Update(entity, key);");
            sw.WriteLine("            }");
            sw.WriteLine("        }");
            sw.WriteLine("");
            sw.WriteLine("        /// <summary>");
            sw.WriteLine("        /// 删除对象");
            sw.WriteLine("        /// </summary>");
            sw.WriteLine("        /// <param name=\"entity\">需要删除的对象</param>");
            sw.WriteLine("        /// <param name=\"session\">NHibernate会话对象</param>");
            sw.WriteLine("        public void Delete(T entity, ISession session)");
            sw.WriteLine("        {");
            sw.WriteLine("            session.Delete(entity);");
            sw.WriteLine("        }");
            sw.WriteLine("");
            sw.WriteLine("        /// <summary>");
            sw.WriteLine("        /// 删除对象数组");
            sw.WriteLine("        /// </summary>");
            sw.WriteLine("        /// <param name=\"entitys\">需要删除的对象数组</param>");
            sw.WriteLine("        /// <param name=\"session\">NHibernate会话对象</param>");
            sw.WriteLine("        public void Delete(IList<T> entitys, ISession session)");
            sw.WriteLine("        {");
            sw.WriteLine("            foreach (T entity in entitys)");
            sw.WriteLine("            {");
            sw.WriteLine("                session.Delete(entity);");
            sw.WriteLine("            }");
            sw.WriteLine("        }");
            sw.WriteLine("");
            sw.WriteLine("        /// <summary>");
            sw.WriteLine("        /// ");
            sw.WriteLine("        /// </summary>");
            sw.WriteLine("        /// <param name=\"hql\"></param>");
            sw.WriteLine("        /// <param name=\"session\"></param>");
            sw.WriteLine("        /// <param name=\"ps\"></param>");
            sw.WriteLine("        /// <returns></returns>");
            sw.WriteLine("        public T UniqueResult(string hql, ISession session, params object[] ps)");
            sw.WriteLine("        {");
            sw.WriteLine("            return (T)this.UniqueObjectResult(hql, session, ps);");
            sw.WriteLine("        }");
            sw.WriteLine("");
            sw.WriteLine("        /// <summary>");
            sw.WriteLine("        /// ");
            sw.WriteLine("        /// </summary>");
            sw.WriteLine("        /// <param name=\"hql\"></param>");
            sw.WriteLine("        /// <param name=\"session\"></param>");
            sw.WriteLine("        /// <param name=\"ps\"></param>");
            sw.WriteLine("        /// <returns></returns>");
            sw.WriteLine("        public object UniqueObjectResult(string hql, ISession session, params object[] ps)");
            sw.WriteLine("        {");
            sw.WriteLine("            IQuery q = session.CreateQuery(hql);");
            sw.WriteLine("            if (ps != null)");
            sw.WriteLine("                for (int i = 0; i < ps.Length; i++)");
            sw.WriteLine("                {");
            sw.WriteLine("                    q.SetParameter(i, ps[i]);");
            sw.WriteLine("                }");
            sw.WriteLine("            return q.UniqueResult();");
            sw.WriteLine("        }");
            sw.WriteLine("");
            sw.WriteLine("        /// <summary>");
            sw.WriteLine("        /// ");
            sw.WriteLine("        /// </summary>");
            sw.WriteLine("        /// <param name=\"hql\"></param>");
            sw.WriteLine("        /// <param name=\"session\"></param>");
            sw.WriteLine("        /// <returns></returns>");
            sw.WriteLine("        public IList<T> Query(string hql, ISession session)");
            sw.WriteLine("        {");
            sw.WriteLine("            return this.Query(hql, 0, 0, session);");
            sw.WriteLine("        }");
            sw.WriteLine("");
            sw.WriteLine("        /// <summary>");
            sw.WriteLine("        /// 通过HQL语句进行查询");
            sw.WriteLine("        /// </summary>");
            sw.WriteLine("        /// <param name=\"hql\">HQL语句查询语句</param>");
            sw.WriteLine("        /// <returns>IList集合对象</returns>");
            sw.WriteLine("        public IList<T> Query(string hql, ISession session, params object[] ps)");
            sw.WriteLine("        {");
            sw.WriteLine("            return this.Query(hql, 0, 0, session, ps);");
            sw.WriteLine("        }");
            sw.WriteLine("");
            sw.WriteLine("        /// <summary>");
            sw.WriteLine("        /// ");
            sw.WriteLine("        /// </summary>");
            sw.WriteLine("        /// <param name=\"hql\"></param>");
            sw.WriteLine("        /// <param name=\"fristResult\"></param>");
            sw.WriteLine("        /// <param name=\"maxResults\"></param>");
            sw.WriteLine("        /// <param name=\"session\"></param>");
            sw.WriteLine("        /// <returns></returns>");
            sw.WriteLine("        public IList<T> Query(string hql, long fristResult, long maxResults, ISession session)");
            sw.WriteLine("        {");
            sw.WriteLine("            return this.Query(hql, fristResult, maxResults, session, new object[0]);");
            sw.WriteLine("        }");
            sw.WriteLine("");
            sw.WriteLine("        /// <summary>");
            sw.WriteLine("        /// 通过HQL语句进行查询");
            sw.WriteLine("        /// </summary>");
            sw.WriteLine("        /// <param name=\"hql\">HQL语句查询语句</param>");
            sw.WriteLine("        /// <returns>IList集合对象</returns>");
            sw.WriteLine("        public IList<T> Query(string hql, long fristResult, long maxResults, ISession session, params object[] ps)");
            sw.WriteLine("        {");
            sw.WriteLine("            IQuery q = session.CreateQuery(hql);");
            sw.WriteLine("            if (ps != null)");
            sw.WriteLine("                for (int i = 0; i < ps.Length; i++)");
            sw.WriteLine("                {");
            sw.WriteLine("                    q.SetParameter(i, ps[i]);");
            sw.WriteLine("                }");
            sw.WriteLine("            if (maxResults > 0)");
            sw.WriteLine("            {");
            sw.WriteLine("                q.SetFirstResult((int)fristResult);");
            sw.WriteLine("                q.SetMaxResults((int)maxResults);");
            sw.WriteLine("            }");
            sw.WriteLine("            return q.List<T>();");
            sw.WriteLine("        }");
            sw.WriteLine("");
            sw.WriteLine("        /// <summary>");
            sw.WriteLine("        /// ");
            sw.WriteLine("        /// </summary>");
            sw.WriteLine("        /// <param name=\"sql\"></param>");
            sw.WriteLine("        /// <param name=\"session\"></param>");
            sw.WriteLine("        /// <returns></returns>");
            sw.WriteLine("        public IList SqlQuery(String sql, ISession session)");
            sw.WriteLine("        {");
            sw.WriteLine("            return this.SqlQuery(sql, 0, 0, session);");
            sw.WriteLine("        }");
            sw.WriteLine("");
            sw.WriteLine("        /// <summary>");
            sw.WriteLine("        /// 通过SQL语句进行查询");
            sw.WriteLine("        /// </summary>");
            sw.WriteLine("        /// <param name=\"sql\"></param>");
            sw.WriteLine("        /// <param name=\"session\"></param>");
            sw.WriteLine("        /// <param name=\"ps\"></param>");
            sw.WriteLine("        /// <returns></returns>");
            sw.WriteLine("        public IList SqlQuery(String sql, ISession session, params Object[] ps)");
            sw.WriteLine("        {");
            sw.WriteLine("            return this.SqlQuery(sql, 0, 0, session, ps);");
            sw.WriteLine("        }");
            sw.WriteLine("");
            sw.WriteLine("        /// <summary>");
            sw.WriteLine("        /// ");
            sw.WriteLine("        /// </summary>");
            sw.WriteLine("        /// <param name=\"sql\"></param>");
            sw.WriteLine("        /// <param name=\"fristResult\"></param>");
            sw.WriteLine("        /// <param name=\"maxResults\"></param>");
            sw.WriteLine("        /// <param name=\"session\"></param>");
            sw.WriteLine("        /// <returns></returns>");
            sw.WriteLine("        public IList SqlQuery(String sql, long fristResult, long maxResults,");
            sw.WriteLine("                ISession session)");
            sw.WriteLine("        {");
            sw.WriteLine("            return this.SqlQuery(sql, fristResult, maxResults, session,");
            sw.WriteLine("                    new Object[0]);");
            sw.WriteLine("        }");
            sw.WriteLine("");
            sw.WriteLine("        /// <summary>");
            sw.WriteLine("        /// 通过HQL语句进行查询");
            sw.WriteLine("        /// </summary>");
            sw.WriteLine("        /// <param name=\"sql\"></param>");
            sw.WriteLine("        /// <param name=\"fristResult\"></param>");
            sw.WriteLine("        /// <param name=\"maxResults\"></param>");
            sw.WriteLine("        /// <param name=\"session\"></param>");
            sw.WriteLine("        /// <param name=\"ps\"></param>");
            sw.WriteLine("        /// <returns></returns>");
            sw.WriteLine("        public IList SqlQuery(String sql, long fristResult, long maxResults,");
            sw.WriteLine("                ISession session, params Object[] ps)");
            sw.WriteLine("        {");
            sw.WriteLine("            IQuery q = session.CreateSQLQuery(sql);");
            sw.WriteLine("            if (ps != null)");
            sw.WriteLine("                for (int i = 0; i < ps.Length; i++)");
            sw.WriteLine("                {");
            sw.WriteLine("                    q.SetParameter(i, ps[i]);");
            sw.WriteLine("                }");
            sw.WriteLine("            if (maxResults > 0)");
            sw.WriteLine("            {");
            sw.WriteLine("                q.SetFirstResult((int)fristResult);");
            sw.WriteLine("                q.SetMaxResults((int)maxResults);");
            sw.WriteLine("            }");
            sw.WriteLine("            return q.List();");
            sw.WriteLine("        }");
            sw.WriteLine("");
            sw.WriteLine("        /// <summary>");
            sw.WriteLine("        /// ");
            sw.WriteLine("        /// </summary>");
            sw.WriteLine("        /// <param name=\"sql\"></param>");
            sw.WriteLine("        /// <param name=\"session\"></param>");
            sw.WriteLine("        /// <returns></returns>");
            sw.WriteLine("        public int SqlExecuteNonQuery(String sql, ISession session)");
            sw.WriteLine("        {");
            sw.WriteLine("            return this.SqlExecuteNonQuery(sql, session, new Object[0]);");
            sw.WriteLine("        }");
            sw.WriteLine("");
            sw.WriteLine("        /// <summary>");
            sw.WriteLine("        /// 执行无返回值的SQL语句");
            sw.WriteLine("        /// </summary>");
            sw.WriteLine("        /// <param name=\"sql\">SQL语句</param>");
            sw.WriteLine("        /// <param name=\"session\">连接状态</param>");
            sw.WriteLine("        /// <param name=\"ps\">参数</param>");
            sw.WriteLine("        /// <returns>影响的行数</returns>");
            sw.WriteLine("        public int SqlExecuteNonQuery(String sql, ISession session, params Object[] ps)");
            sw.WriteLine("        {");
            sw.WriteLine("            IDbConnection connection = session.Connection;");
            sw.WriteLine("            IDbCommand cmd = connection.CreateCommand();");
            sw.WriteLine("            cmd.CommandText = sql;");
            sw.WriteLine("            if (ps != null)");
            sw.WriteLine("                for (int i = 0; i < ps.Length; i++)");
            sw.WriteLine("                {");
            sw.WriteLine("                    cmd.Parameters.Add(ps[i]);");
            sw.WriteLine("                }");
            sw.WriteLine("            return cmd.ExecuteNonQuery();");
            sw.WriteLine("        }");       
            sw.WriteLine("    }");
            sw.WriteLine("}");
            sw.Close();
            file.Close();
            #endregion
        }
    }
}
